package com.zhijin.empty.Interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhijin.empty.Util.logUtil;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class MyInterceptor implements HandlerInterceptor {
    private static logUtil log = logUtil.getInst(MyInterceptor.class);

    /**
     *  请求前的处理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("\n------------------------------------------------拦截器 -> 请求前的处理------------------------------------------------");
        Map requestMap = request.getParameterMap();
        JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(requestMap));
        System.out.println(JSONObj.toString());
        return true;
    }

    /**
     * 请求时的处理
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        log.info("\n------------------------------------------------拦截器 -> 请求时的处理------------------------------------------------");
    }

    /**
     * 请求后的处理
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        log.info("\n------------------------------------------------拦截器 -> 请求后的处理------------------------------------------------");
    }
}
